package com.example.springtxjms.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class CustomerService {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static final String NEW_MSG = "customer:msg1:new";
    public static final String REPLY_MGS = "customer:msg:reply";

    @Transactional
    @JmsListener(destination = "customer:msg1:new",containerFactory = "msgFactory")
    public void handle(String msg) {
        log.info("Get msg:[{}]", msg);
        String reply = "Reply1-" + msg;
        jmsTemplate.convertAndSend(REPLY_MGS, reply);
        if (msg.contains("error")) {
            simulateError();
        }
    }

    private void simulateError() {
        throw new RuntimeException("some Data error.");
    }
}
